<template>
  <div id="attend">
    <div class="top">
      <div class="top—left">
        <div class="name" style="font-size: 16rem">
          {{ info.name }}
        </div>
        <div class="phone" style="font-size: 14rem; margin-top: 5rem">
          {{  info.phone }}
        </div>
      </div>
      <van-button plain type="primary" size="small" @click="toUpdate"
        >修改个人信息</van-button
      >
    </div>
    <div
      style="
        width: 100%;
        flex: 1;
        display: flex;
        align-items: center;
        flex-flow: row;
      "
    >
      <div class="center_attend">
        <div class="center_text" v-if="attend == null" @click="clickAttend">
          <div>立即打卡</div>
          <div style="margin: 10rem">{{ this.current_time }}</div>
        </div>
        <div class="center_text" v-else>
          <div>今日已打卡</div>
          <div style="margin: 10rem">{{ attend.createTime }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Notify } from "vant";

export default {
  name: "Attend",
  data() {
    return {
      loading: false,
      finished: true,
      attend: null,
      info: {},
      current_time: new Date().Format("yyyy/MM/dd hh:mm:ss"),
    };
  },
  created() {
    this.getUserInfo();
    this.getAttendInfo();
    let that = this;
    setInterval(
      function () {
        that.current_time = new Date().Format("yyyy/MM/dd hh:mm:ss");
      },
      1000,
      1000
    );
  },
  methods: {
    getUserInfo() {
      let that = this;
      this.$http.get("/api/getUserInfo").then(function (data) {
        if (data.code === 0) {
          that.info = data.data;
        } else {
          Notify({ type: "warning", message: "获取信息失败：" + data.msg });
        }
      });
    },
    toUpdate() {
      this.$router.push({
        name: "Info",
        params: {
          userInfo: this.info,
        },
      });
    },
    getAttendInfo() {
      let that = this;
      this.$http.get("/api/attend/get").then(function (data) {
        if (data.code === 0) {
          that.attend = data.data;
        } else {
          Notify({ type: "warning", message: "获取打卡信息失败：" + data.msg });
        }
      });
    },
    clickAttend() {
      let that = this;
      this.$http.post("/api/attend").then(function (data) {
        if (data.code === 0) {
          that.attend = data.data;
        } else {
          Notify({ type: "warning", message: "打卡失败：" + data.msg });
        }
      });
    },
  },
};
</script>

<style scoped>
.center_text {
  width: auto;
  height: auto;
  margin: auto;
  display: flex;
  flex-flow: column;
  align-items: center;
}

.center_attend {
  width: 150rem;
  height: 150rem;
  border-radius: 50%;
  margin: auto;
  background: #07c160;
  color: white;
  align-items: center;
  display: flex;
  flex-flow: column;
}

.top—left {
  flex: 1;
  display: flex;
  flex-flow: column;
}

.top {
  width: 100%;
  display: flex;
  box-sizing: border-box;
  background: #f0f0f0;
  padding: 10rem;
  align-items: center;
  align-content: space-between;
}

#attend {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: column;
  align-items: center;
}
</style>
